
import AbsWindow from "../../framework/AbsWindow";
import { WindowName } from "../../framework/reflect/WindowName";
import GameWindow from "./GameWindow";
import SettlementWindow from "./SettlementWindow";
import Context from "../../framework/Context";
import LocalStorage from "../../framework/utils/LocalStorage";
import { WxEvent } from "../../framework/wx/WxEvent";
const { ccclass, property } = cc._decorator;
@ccclass
@WindowName("FinishWindow")
export default class FinishWindow extends AbsWindow {

    @property(cc.Node)
    private returnBtn: cc.Node = null;
    @property(cc.Label)
    private awardLabel:cc.Label = null;
    @property(cc.Label)
    private baseLabel:cc.Label = null;
    @property(cc.Label)
    private weaponInfo:cc.Label = null;
    @property(cc.Label)
    private weaponName:cc.Label = null;
    private id = 0;
    public static sendData(data,count:number): Promise<FinishWindow> {
        return Context.get().startWindow(FinishWindow).then(finish => {
            finish.initData(data,count);
            cc.log("FinishWindow sendData",data,count);
            return Promise.resolve(finish)
        })
    }
    public initData(data,count:number) {
        this.awardLabel.string = String(count);
        this.baseLabel.string = String(data[0]['idle_reward']*data[1]);
        this.weaponName.string = data[0]['name'];
        this.weaponInfo.string = data[0]['desc'];
        this.id = data[0]["id"];
        var gold = count+Number(data[0]['idle_reward']*data[1]);
        var curPlayerData:any =  LocalStorage.getObj("Player");
        var curgold =curPlayerData.gold;
        var oneselfWeapon = curPlayerData["oneselfWeapon"];
        var total:number = gold+curgold;
        cc.log("total",total,gold,curgold)
        var playerData = {
            "gold":total,
            oneselfWeapon:oneselfWeapon
        }
        LocalStorage.setObj("Player",playerData);
        Context.get().messenger.sendMessage(WxEvent.ON_CHANGEGOLD,[total]);
    }
    public onStart() {
        this.setClickListener(this.returnBtn, this.onReturnBtn);
    }

    public listen(): string[] {
        return []
    }
    public handleMessage(message: string, args: any[]): boolean {
        switch (message) {
        }
        return false
    }
    private onReturnBtn() {
        var fbData:any = LocalStorage.getObj("FBData");
        var time = cc.sys.now();
        for(let index in fbData){
            if(fbData[index]["id"] ==0){
                fbData.splice(Number(index),1,{"id": this.id, "startTime": time});
               break;
            }
        }
        LocalStorage.setObj("FBData",fbData); 
        this.context.closeWindow(FinishWindow);
        this.context.closeWindow(GameWindow);
    }
}